﻿CREATE PROCEDURE [acms].[TopicGroup_Put]
	@Id int, 
	@SiteId int,
	@Name varchar(50),
	@Path varchar(50),
	@Notes varchar(1000),
	@Created DateTime,
	@ConcurencyId RowVersion,
	@IdOut int output,
	@ConcurencyIdOut RowVersion output
AS

	if(@Id is null or @Id = 0) begin
		insert into TopicGroup
			(SiteId, [Name], Path, Notes, Created)
		values
			(@SiteId, @Name, @Path, @Notes, @Created)
		set @IdOut = @@Identity
	end
	else if exists(Select null from TopicGroup with(nolock) where Id = @Id and ConcurencyId = @ConcurencyId and Deleted = 0) begin
		update TopicGroup
		set
			SiteId = @SiteId,
			Name = @Name,
			Path = @Path, 
			Notes = @Notes
		where 
			Id = @Id 
			and ConcurencyId = @ConcurencyId 
			and Deleted = 0
		set @IdOut = @Id
	end
	else if exists(Select null from TopicGroup with(nolock) where Id = @Id and Deleted = 0) begin
		return -100
	end
	else begin
		return -200
	end
	
	select @ConcurencyIdOut = ConcurencyId From TopicGroup_View Where Id = @IdOut
RETURN 0;